home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / test / bmachinetest.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  6.7 KB  |  149 lines

  1. import unittest
  2. import os
  3. import os.path
  4. import glob
  5. from copy import copy
  6.  
  7. import httpclient
  8. import eventloop
  9.  
  10. from test.framework import DownloaderTestCase
  11.  
  12. # In order to use this test, you must set the following environment variables
  13. #
  14. # BMACHINE_SERVER - the ssh location of the machine where you wish
  15. #                   to install Broadcast Machine for testing ie
  16. #                   "user@test.getdemocracy.com"
  17. #
  18. # BMACHINE_SERVER_LOCATION - the ssh location of the directory where
  19. #                            you wish to install Broadcast Machine for
  20. #                            testing. For example "/bmachine" will
  21. #                            result in sshing to
  22. #                            user@test.getdemocracy.com:/bmachine
  23. #
  24. # BMACHINE_SERVER_URL - the url of the above directory
  25. #                       ie "http://test.getdemocracy.com/bmachine"
  26. #
  27. #
  28. # BMACHINE_LOCATION - the location of Broadcast Machine on the testing
  29. #                     computer ie "/home/user/bmachine"
  30. #
  31.  
  32. ADMIN_ONLY_PAGES = ['admin.php', 'channels.php', 'create_channel.php', 'delete.php','donation.php','donations.php', 'edit_channel.php',  'edit_videos.php', 'generate_htaccess.php', 'pause.php', 'start.php', 'stop.php', 'users.php', 'user_edit.php', 'settings.php']
  33.  
  34. class BroadcastMachineTest(DownloaderTestCase):
  35.     def setUp(self):
  36.         self.assert_(os.environ.has_key('BMACHINE_SERVER'))
  37.         self.assert_(os.environ.has_key('BMACHINE_SERVER_LOCATION'))
  38.         self.assert_(os.environ['BMACHINE_SERVER_LOCATION'].startswith('/'))
  39.         self.assert_(os.environ.has_key('BMACHINE_SERVER_URL'))
  40.         self.assert_(os.environ.has_key('BMACHINE_LOCATION'))
  41.         self.loc = os.environ['BMACHINE_LOCATION']
  42.         self.server = os.environ['BMACHINE_SERVER']
  43.         self.server_loc = os.environ['BMACHINE_SERVER_LOCATION']
  44.         self.url = os.environ['BMACHINE_SERVER_URL']
  45.         DownloaderTestCase.setUp(self)
  46.         self.dlError = False
  47.         self.fixedPermissions = False
  48.         
  49.     def test(self):
  50.         launchArgs = ["-C", "-q","-r"]
  51.         launchArgs.extend(glob.glob(os.path.join(self.loc, '*')))
  52.         launchArgs.append("%s:%s" % (self.server, self.server_loc))
  53.         self.assertEqual(os.spawnvp(os.P_WAIT, "scp", launchArgs),0)
  54.         httpclient.grabURL (self.url, self.setupCallback, self.errorCallback)
  55.         self.runEventLoop()
  56.  
  57.     def fixPermissions(self):
  58.         self.assert_(not self.fixedPermissions)
  59.         self.fixedPermissions = True
  60.         for d in ["torrents", "data", "publish", "thumbnails", "text"]:
  61.             os.spawnlp(os.P_WAIT, "ssh", "-C", self.server, 'mkdir %s 2>&1' % os.path.join(self.server_loc, d))
  62.             os.spawnlp(os.P_WAIT, "ssh", "-C", self.server, 'chmod 777 %s 2>&1' % os.path.join(self.server_loc, d))
  63.  
  64.     def setupCallback(self, info):
  65.         self.info = info
  66.         self.assertEqual(info['status'], 200)
  67.         if info['body'].find("Before you can use Broadcast Machine, we need to create a few directories.  There are several ways to do this listed below.") != -1:
  68.             self.fixPermissions()
  69.             httpclient.grabURL (self.url, self.setupCallback, self.errorCallback)
  70.         else:
  71.             self.assertNotEqual(info['body'].find("This looks like your first time using Broadcast Machine."), -1)
  72.             self.assertNotEqual(info['body'].find("You should create a new user account before continuing."), -1)
  73.             httpclient.grabURL(info['redirected-url'], self.createAccountCallback,
  74.                                self.errorCallback, method="POST",
  75.                                postVariables = {"do_login" : "1",
  76.                                                 "email": "nassar@pculture.org",
  77.                                                 "username": "admin",
  78.                                                 "pass1": "PCF is cool",
  79.                                                 "pass2": "PCF is cool"})
  80.  
  81.     def makeBMCookies(self):
  82.         return {'PHPSESSID':self.PHPSESSID,
  83.                 'bm_userhash': self.bm_userhash,
  84.                 'bm_username': self.bm_username}
  85.  
  86.     def storeBMCookie(self, cookies):
  87.         if cookies.has_key('bm_userhash'):
  88.             self.bm_userhash = cookies['bm_userhash']
  89.         if cookies.has_key('bm_username'):
  90.             self.bm_username = cookies['bm_username']
  91.         if cookies.has_key('PHPSESSID'):
  92.             self.PHPSESSID = cookies['PHPSESSID']
  93.  
  94.     def createAccountCallback(self, info):
  95.         self.assertNotEqual(info['body'].find("The account admin was added successfully."), -1)
  96.         self.assertEqual(info['cookies']['bm_username']['Value'],'admin')
  97.         self.storeBMCookie(info['cookies'])
  98.         self.loadNoCookies(None, ADMIN_ONLY_PAGES)
  99.  
  100.     def loadNoCookies(self, info, urllist):
  101.         if info is not None:
  102.             self.assert_(info['redirected-url'].endswith('/index.php'))
  103.         if len(urllist) > 0:
  104.             nexturl = self.url + urllist.pop()
  105.             httpclient.grabURL(nexturl,
  106.                                lambda info: self.loadNoCookies(info, urllist),
  107.                                self.errorCallback)
  108.         else:
  109.             httpclient.grabURL(self.url+'create_channel.php',
  110.                                self.createChannelLoad,
  111.                                self.errorCallback, method="POST",
  112.                                cookies = self.makeBMCookies(),
  113.  
  114.                                postVariables = {
  115.                 'Name' : 'Test Channel 1',
  116.                 'Description' : 'This is a test channel',
  117.                 'Publisher' : 'Democracy/Broadcast Machine tester',
  118.                 'Icon' : 'http://images.slashdot.org/topics/topiccommunications.gif',
  119.                 'post_use_auto' : "1",
  120.                 'Options[Thumbnail]' : "1",
  121.                 'Options[Title]' : "1",
  122.                 'Options[Creator]' : "1",
  123.                 'Options[Description]' : "1",
  124.                 'Options[Length]' : "1",
  125.                 'Options[Filesize]' : "1",
  126.                 'Options[Torrent]' : "1",
  127.                 'Options[URL]' : "1",
  128.                 'Options[Keywords]' : "1",
  129.                 'SubscribeOptions[0]' : "1",
  130.                 'SubscribeOptions[1]' : "2",
  131.                 'SubscribeOptions[2]' : "4",
  132.                 
  133.                 })
  134.         
  135.  
  136.     def createChannelLoad(self, info):
  137.         self.assert_(info['redirected-url'].endswith('/channels.php'))
  138.         
  139.         self.assertNotEqual(info['body'].find('Test Channel 1'), -1)
  140.         self.assertNotEqual(info['body'].find('Democracy/Broadcast Machine tester'), -1)
  141.         self.assertNotEqual(info['body'].find('http://images.slashdot.org/topics/topiccommunications.gif'), -1)
  142.         self.storeBMCookie(info['cookies'])
  143.         eventloop.quit()
  144.  
  145.     def errorCallback(self, error):
  146.         print "Broadcast Machine URL load died with %s" % error
  147.         self.dlError = True
  148.         self.stopEventLoop()
  149.